/**
 * @file   main.cpp
 * @author ywl <ywl@ywl-VirtualBox>
 * @date   Sun Oct 16 13:31:47 2022
 * 
 * @brief  An implementation of BST sorting algorithm
 * 
 * 
 */

#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include "BinarySearchTree.h"
using namespace std;

/** 
 * Print the elements of vector by desired style.
 * 
 * @param vec 
 */
template <typename T>
void print(vector<T>& vec)
{
  cout << "{ ";
  for (const T & n : vec)
      cout << n << ", ";
  cout << "\b\b" << " }" << endl << endl;  
}

    // Test program
int main(int argc, char *argv[])
{
    // Create a Binary Search Tree
    BinarySearchTree<int> bst;

    ifstream file;
    time_t start;
    time_t end;
    
    // Open the file containing randomly ordered 
    file.open("number.txt");
    int temp;
    vector<int> vec(0);
    while(!file.eof()){
        file >> temp;
        vec.push_back(temp);
    }

    cout << "Size: " << vec.size() << endl;
    cout << "Primary Sequence: " << endl;
    print(vec);

    /**
     * Maintain initial order and sort.
     */
    int mode = 0;
    cout << "BST_sort(mode = 0): " << endl;
    start = clock();
    BSTSorting(vec, mode);
    end = clock();
    
    // Calculate running time(measured in second)
    double runtime = (double) (end - start) / CLOCKS_PER_SEC;
    cout << "Time consuming: " << runtime <<  "s" << endl;

    cout << "Result of BST_sort(mode = 0): " << endl;
    print(vec);

    /**
     * Disrupt order and sort.
     */
    mode = 1;
    cout << "BST_sort(mode = 1): " << endl;
    start = clock();
    BSTSorting(vec, mode);
    end = clock();
    
    // Calculate running time(measured in second)
    runtime = (double) (end - start) / CLOCKS_PER_SEC;
    cout << "Time consuming: " << runtime << "s" << endl;

    cout << "Result of BST_sort(mode = 1): " << endl;
    print(vec);

    /**
     * Test BST_sort on above sorted sequence.
     */
    mode = 0;
    cout << "BST_sort(mode = 0) over sorted sequence: " << endl;
    start = clock();
    BSTSorting(vec, mode);
    end = clock();
    
    // Calculate running time(measured in second)
    runtime = (double) (end - start) / CLOCKS_PER_SEC;
    cout << "Time consuming: " << runtime << "s" << endl;

    cout << "Result of BST_sort: " << endl;
    print(vec);

    return 0;
}
